package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import TO.EdicaoTO;
import TO.EditoraTO;

public class EditoraDAO {
	
	private static SessionFactory fabrica;
	
	public EditoraDAO(){
		fabrica = new Configuration().configure("TO/hibernate.cfg.xml").buildSessionFactory();
	}
	
	public boolean inserir(EditoraTO editora) {
		
		try {
			Session sessao = fabrica.openSession();
						
			Transaction transaction = sessao.beginTransaction();
			sessao.save(editora);
			
			transaction.commit();
			sessao.close();
			
			return true;
			
		} catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}		
		
	}
	
	public static Collection<EditoraTO>  consultar(String nome){
		 
		Collection<EditoraTO> list = new ArrayList<EditoraTO>(); 
		try {
		    Session session = fabrica.openSession();     
		    
		    Query q = session.createQuery("from EditoraTO e where e.nome LIKE :nome");
		    q.setString("nome", nome);
		    		    
		    list = q.list();  
		    session.flush();     
		    session.close(); 
			
		} catch(Exception e)
			{System.out.println(e.getMessage());
		}
		
		return list;
	}
	

}
